<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$postdata=get_postdata();
$access_token = cfc('account')->getAccessToken();
$media_id = $postdata['media_id'];
if(empty($media_id)){
	$return['error'] = 1;
	$return['message'] = '获取微信媒体参数失败！';
	exi($return);
}
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;
$updir =ROOT_AT ."/images/".$_SESSION['uniacid']."/".date("Y",time())."/".date("m",time())."/";
if (!file_exists($updir)) {
    mkdir($updir, 0777, true);
}
$randimgurl = "images/".$_SESSION['uniacid']."/".date("Y",time())."/".date("m",time())."/".date('YmdHis').rand(1000,9999).'.jpg';
$targetName = ROOT_AT .$randimgurl;
$ch = curl_init($url); // 初始化
$fp = fopen($targetName, 'wb'); // 打开写入
curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置，值是一个资源类型
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
if(file_exists($targetName)){
			$return['error'] = 0;			
			cfc('files')->file_image_crop($targetName,$targetName,640,'');
			$return['realimgurl'] = $randimgurl;
			$return['imgurl'] = $randimgurl;
			$return['message'] = '上传成功';
}else{
			$return['error'] = 1;
			$return['message'] = '上传失败';
}

exi($return);